package com.csc.training.action;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.csc.training.DAO.ClassDAOImpl;
import com.csc.training.DAO.StudentDAOImpl;
import com.csc.training.bean.Class;
import com.csc.training.bean.Student;
import com.csc.training.inf.ClassDAO;
import com.csc.training.inf.StudentDAO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class StudentAction extends ActionSupport implements
		ModelDriven<Student>, ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private Student student = new Student();
	private StudentDAO studentDAO = new StudentDAOImpl();
	private ClassDAO classDAO = new ClassDAOImpl();
	private List<Student> listStudent = new ArrayList<Student>();
	private List<Class> listClass = new ArrayList<Class>();
	private String dateOfBirth;
	private String findName;

	public List<Class> getListClass() {
		return listClass;
	}

	public void setListClass(List<Class> listClass) {
		this.listClass = listClass;
	}

	public String getFindName() {
		return findName;
	}

	public void setFindName(String findName) {
		this.findName = findName;
	}

	private String className;

	public String getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(String dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public List<Student> getListStudent() {
		return listStudent;
	}

	public void setListStudent(List<Student> listStudent) {
		this.listStudent = listStudent;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	@Override
	public Student getModel() {
		return student;
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String addStudent() throws Exception {
		Class classStudent = classDAO.getClassByName(className);
		Date date = new Date(dateOfBirth);
		student.setBirthday(date);
		student.setClassStudent(classStudent);
		studentDAO.addStudent(student);
		listStudent = null;
		listStudent = studentDAO.listStudent();

		return SUCCESS;
	}

	public String listStudent() throws Exception {
		listStudent = null;
		listStudent = studentDAO.listStudent();
		return SUCCESS;
	}

	public String searchStudent() throws Exception {
		listStudent = null;
		listStudent = studentDAO.findStudentByName(findName);
		return SUCCESS;
	}

	public String forwardAddNew() throws Exception {
		listClass = classDAO.listAllClass();
		return SUCCESS;
	}

	public String prepareUpdate() {
		long studentid = Long.parseLong(request.getParameter("studentId"));
		student = studentDAO.getStudentById(studentid);
		listClass = classDAO.listAllClass();
		className = student.getClassStudent().getClassName();
		Format formatter = new SimpleDateFormat("dd-MMM-yyyy");

		dateOfBirth = formatter.format(student.getBirthday());
		return SUCCESS;
	}

	public String updateStudent() {
		Class classStudent = classDAO.getClassByName(className);
		Date date = new Date(dateOfBirth);
		student.setBirthday(date);
		student.setClassStudent(classStudent);
		studentDAO.updateStudent(student);
		listStudent = null;
		listStudent = studentDAO.listStudent();
		return SUCCESS;
	}

	public String deleteStudent() {
		long studentid = Long.parseLong(request.getParameter("studentId"));
		student = studentDAO.getStudentById(studentid);
		studentDAO.deleteStudent(student);
		return SUCCESS;
	}

	public String viewStudent() {
		long studentid = Long.parseLong(request.getParameter("studentId"));
		student = studentDAO.getStudentById(studentid);

		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

}
